#include "cnpipe_actions.h"
#include "cnpipe_actionsofdisplayingshapes.h"
#include "cnpipe_interfaces_window.h"
#include "cnpipe_jsonhandler.h"
#include "corefunctionwidget.h"
#include "pipedisplayopenglwidget.h"
#include "myglwidget.h"
#include "stdio.h"

#include <QAction>
#include <QByteArray>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QList>
#include <QMenuBar>
#include <QMessageBox>
#include <QOpenGLWidget>
#include <QSizePolicy>
#include <QSlider>
#include <QSpinBox>
#include <QString>
#include <QTextStream>

CNPIPE_interfaces_window::CNPIPE_interfaces_window(QWidget *parent) :
    QMainWindow (parent)
{
    /** 在窗体中显示OpenGL的组件，以及各种滑块。
     * 这是CNPIPE项目的测试版，后期会根据项目的要求进行修改。 */
    setWindowTitle("Display window of CNPIPE");
    resize(1024, 768);

    //openGL显示对象，继承自MyOpenGLWidget，专门用于显示openGL图形
    //CoreFunctionWidget *openGL = new CoreFunctionWidget(this);
    openGL = new PipeDisplayOpenGLWidget(this);

    openGL->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    openGL->resize(200, 200);

    QSlider *slider1 = new QSlider(Qt::Horizontal, this);
    slider1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    QSlider *slider2 = new QSlider(Qt::Horizontal, this);
    slider2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(slider1);
    vLayout->addWidget(slider2);

    QHBoxLayout *layout = new QHBoxLayout;
    this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    layout->addWidget(openGL);
    layout->addLayout(vLayout);

    QWidget *windowWidget = new QWidget(this);
        windowWidget->setLayout(layout);
        setCentralWidget(windowWidget);

    this->addMenus();
}

//初始化上面的菜单
CNPIPE_interfaces_window::~CNPIPE_interfaces_window()
{

    delete ui;
}

void CNPIPE_interfaces_window::addMenus() {
    qDebug() << "initialize menu";

    openAction = new QAction(QIcon(":/images/doc-open"), tr("&打开任意一个JSON格式文件..."), this);
    openAction->setStatusTip(tr("打开任意一个JSON格式文件，这是CAPS 3.0中的做法。　"));
    connect(openAction, SIGNAL(triggered()), this, SLOT(openJSONFile()));

    exportAction = new QAction(QIcon(":/images/doc-open"), tr("&导出..."), this);

    /** 文件总菜单 */
    QMenu *file = menuBar()->addMenu(tr("&文件"));
    file->addAction(openAction);
    file->addAction(exportAction);

//    QString text = "揽炒派";
//    QByteArray ba = text.toUtf8(); // must
//    char *ch=ba.data();

    /** 显示方式总菜单 */
    QMenu *displayStyle = menuBar()->addMenu(tr("&显示方式"));
    pipeLineDisplayAction = new QAction(QIcon(":/images/doc-open"), tr("&管道结构"), this);
    connect(pipeLineDisplayAction, SIGNAL(triggered()), this, SLOT(showPipeLine()));

    realPipeDisplayAction = new QAction(QIcon(":/images/doc-open"), tr("&实体管道"), this);
    connect(realPipeDisplayAction, SIGNAL(triggered()), this, SLOT(showRealPipe()));

    displayStyle->addAction(pipeLineDisplayAction);
    displayStyle->addAction(realPipeDisplayAction);

    /** 添加图形总菜单 */
    QMenu *addShapesMenu = menuBar()->addMenu(tr("&添加图形"));
    //添加立方体
    showCubeAction = new QAction(QIcon(":/images/doc-open"), tr("&添加立方体　"), this);
    showCubeAction->setStatusTip(tr("在图形中添加立方体。　"));
    connect(showCubeAction, SIGNAL(triggered()), this, SLOT(showCube()));
    addShapesMenu->addAction(showCubeAction);
    //添加球体
    showSphereAction = new QAction(QIcon(":/images/doc-open"), tr("&添加球体　"), this);
    showSphereAction->setStatusTip(tr("在图形中添加球体。　"));
    connect(showSphereAction, SIGNAL(triggered()), this, SLOT(showSphere()));
}

/** 视图渲染的方法 */
void CNPIPE_interfaces_window::render() {

}

/** 在文件管理器中读取管道展示专用JSON文件 */
void CNPIPE_interfaces_window::openJSONFile() {
    CNPIPE_jsonHandler *jh = (CNPIPE_jsonHandler *) parentInterface->getJSONHandler();
    jh->openJSONFile();
}

/** 显示管道结构 */
void CNPIPE_interfaces_window::showPipeLine() {
    CNPIPE_actions *action = (CNPIPE_actions*) parentInterface->getAction();
    action->showPipeline();
}

/** 显示实体管道 */
void CNPIPE_interfaces_window::showRealPipe() {
    CNPIPE_actions *action = (CNPIPE_actions*) parentInterface->getAction();
    action->showRealPipe();
}

/** 显示立方体 */
void CNPIPE_interfaces_window::showCube() {
    CNPIPE_actionsOfDisplayingShapes *action2
            = (CNPIPE_actionsOfDisplayingShapes*) parentInterface->getActionsOfDisplayingShapes();

    action2->showCube();
}

/** 显示球体 */
void CNPIPE_interfaces_window::showSphere() {
    CNPIPE_actionsOfDisplayingShapes *action2
            = (CNPIPE_actionsOfDisplayingShapes*) parentInterface->getActionsOfDisplayingShapes();

    action2->showSphere();
}
